package cn.iocoder.yudao.module.system.service.memorialapply;

import cn.iocoder.yudao.module.system.controller.admin.memorialapply.vo.MemorialApplyPageReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.memorialapply.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.memorialapply.MemorialApplyDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.memorialapply.MemorialApplyMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 馆申请记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MemorialApplyServiceImpl implements MemorialApplyService {

    @Resource
    private MemorialApplyMapper applyMapper;

    @Override
    public Long createApply(MemorialApplySaveReqVO createReqVO) {
        // 插入
        MemorialApplyDO apply = BeanUtils.toBean(createReqVO, MemorialApplyDO.class);
        applyMapper.insert(apply);

        // 返回
        return apply.getId();
    }

    @Override
    public void updateApply(MemorialApplySaveReqVO updateReqVO) {
        // 校验存在
        validateApplyExists(updateReqVO.getId());
        // 更新
        MemorialApplyDO updateObj = BeanUtils.toBean(updateReqVO, MemorialApplyDO.class);
        applyMapper.updateById(updateObj);
    }

    @Override
    public void deleteApply(Long id) {
        // 校验存在
        validateApplyExists(id);
        // 删除
        applyMapper.deleteById(id);
    }

    @Override
        public void deleteApplyListByIds(List<Long> ids) {
        // 删除
        applyMapper.deleteByIds(ids);
        }


    private void validateApplyExists(Long id) {
        if (applyMapper.selectById(id) == null) {
            throw exception(APPLY_NOT_EXISTS);
        }
    }

    @Override
    public MemorialApplyDO getApply(Long id) {
        return applyMapper.selectById(id);
    }

    @Override
    public PageResult<MemorialApplyDO> getApplyPage(@Valid MemorialApplyPageReqVO pageReqVO) {
        return applyMapper.selectPage(pageReqVO);
    }

}